
-- The enemy is the final boss of the stage
-- They jump and shoot at the hero
-- If defeated the hero can pass to another stage

enemy = {
  initialX = 400,
  x = 400,
  y = love.graphics.getHeight() -95 - (96/2),
  -- Sprite and Frame Variables
  spriteX = 70,
  spriteY = 97,
  frame = 0,
  frametime = 0,
  charQuads = {},
  charImage = love.graphics.newImage("images/sprites/enemy.png"),
  -- Moving Variables
  speed = 30,
  direction = 2, -- Frontal Direction
  -- Jump Variables
  jumpTime = 0,
  jump = false,
  -- Boost Variables
  boost = false,
  boostTime = 0,
  -- Knife Variables
  knifes = {},
  -- Game Variables
  life = 100,

  loadImage = function()
    local count = 1  
    for j = 0, 3, 1 do
      for i = 0, 2, 1 do
        if(enemy.charQuads[j] == nil) then 
          enemy.charQuads[j] = {}
        end
        enemy.charQuads[j][i] = love.graphics.newQuad(i * enemy.spriteX, j * enemy.spriteY, enemy.spriteX, enemy.spriteY, enemy.charImage:getWidth(), enemy.charImage:getHeight())
        count = count + 1
      end
    end
  end,

  animate = function(dt)
    enemy.frametime = enemy.frametime + dt
    if enemy.frametime > 0.1 then      
      enemy.frame = enemy.frame + 1       
      enemy.frametime = 0
    end    
    if enemy.charQuads[enemy.direction][enemy.frame] == nil then
      enemy.frame = 0
    end
  end,

  move = function(dir, dt)  
    if dir == "down" then
      enemy.direction = 2
      --So character does not pass the ground
      if(enemy.y <= love.window.getHeight() - 95) then -- 115 is the floor size
        enemy.y = enemy.y + enemy.speed * dt
      end
    elseif dir == "up" then
      enemy.direction = 0
      --Removed so character does not fly
      --hero.y = hero.y - hero.speed * dt          
    elseif dir == "left" then
      enemy.direction = 3
      enemy.x = enemy.x - enemy.speed * dt
    elseif dir == "right" then
      enemy.direction = 1
      enemy.x = enemy.x + enemy.speed * dt
    end  
    enemy.animate(dt) 
  end,

  updateGround = function(dt)
    if(enemy.y <= love.window.getHeight() - 95) then
      enemy.y = enemy.y + (100 * dt)
    end
  end,

  checkJump = function(dt)
    if enemy.jump then
      enemy.y = enemy.y - (300 * dt)
      enemy.jumpTime = enemy.jumpTime + dt
      if enemy.jumpTime > 0.3 then
        enemy.jumpTime = 0
        enemy.jump = false
      end
    end
  end,

  checkVelocity = function(dt)
    if enemy.boost then
      enemy.speed = enemy.speed + 20
      enemy.boostTime = enemy.boostTime + dt
      if enemy.boostTime > 0.5 then
        enemy.boostTime = 0
        enemy.boost = false
        enemy.speed = 100
      end
    end
  end,

}